package com.boctech.yxt.commonsutils.exception;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class AjaxResponse {

    // 响应元数据
    private MetaData meta;

    // 具体的响应数据
    private Object data;

    // 出现错误时的响应方法，此方法只能处理自定义异常
    public static AjaxResponse error(CustomException e){
        AjaxResponse response = new AjaxResponse();
        MetaData meta = new MetaData(e.getCode(), e.getMessage());
        response.setMeta(meta);
        return response;
    }

    // 出现错误时的响应方法，可以处理所有异常
    public static AjaxResponse error(ExceptionType exceptionType, String message){
        CustomException exception = new CustomException(exceptionType, message);
        return error(exception);
    }

    // 成功时的响应，不需要返回数据
    public static AjaxResponse success(int status){
        AjaxResponse response = new AjaxResponse();
        MetaData meta = new MetaData(status, "成功");
        response.setMeta(meta);
        return response;
    }

    // 成功时的响应，返回数据
    public static AjaxResponse success(int status, Object data){
        AjaxResponse response = success(status);
        response.setData(data);
        return response;
    }
}
